//
//  HomeViewController.m
//  UI_A项目
//
//  Created by dlios on 15-1-15.
//  Copyright (c) 2015年 songCheng. All rights reserved.
//

#import "HomeViewController.h"

@interface HomeViewController ()

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    
    
    
    

    
    AFNetworkReachabilityManager *albity = [AFNetworkReachabilityManager sharedManager];
    
    
    
    NSString *path = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/super_explore_index?device=iPhone&includeActivity=true&picVersion=5&scale=2"];
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    
    
    [manage GET:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [albity stopMonitoring];
        
        NSDictionary *dic = responseObject;
//        NSLog(@"%@", dic);
        NSDictionary *dic1 = [dic objectForKey:@"categories"];
        
        NSMutableArray *array = [dic1 objectForKey:@"data"];
        self.homeArr = [NSMutableArray array];
        
        for (NSDictionary *dicc in array) {
            
            HomeModel *model = [[HomeModel alloc] init];
            
            model.name = [dicc objectForKey:@"name"];
            model.title = [dicc objectForKey:@"title"];
           
            
            
            
            [self.homeArr addObject:model];
            
        }
        [self.collection reloadData];
//                NSLog(@"sssssss%@",self.homeArr);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];
    
    
    UICollectionViewFlowLayout *layOut = [[UICollectionViewFlowLayout alloc]init];
    
    layOut.itemSize = CGSizeMake(100, 30);
    
    layOut.minimumInteritemSpacing = 1;
    
    layOut.minimumLineSpacing = 40;
    
    layOut.sectionInset = UIEdgeInsetsMake(5, 22, 10, 22);
    
    layOut.footerReferenceSize = CGSizeMake(0, 80);
    
    
    
    
    
    
    self.collection = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:layOut];
   
    self.collection.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:@"u=213802053,255796087&fm=21&gp=0" ofType:@"jpg"]]];
    
     self.collection.alpha = 0.8;
    
    self.collection.delegate = self;
    
    self.collection.dataSource = self;
    
    [self.view addSubview:self.collection];
    
    
    [self.collection registerClass:[HomeButtonCollectionViewCell class] forCellWithReuseIdentifier:@"HOTID1"];
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
   
    
    
    HomeModel *model = [self.homeArr objectAtIndex:indexPath.row];     homeInformationViewController *view1 = [[homeInformationViewController alloc] init];
    view1.model = model;
   
//    NSLog(@"sss%@", model);
    [_home.navigationController pushViewController:view1 animated:YES];
    
    
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.homeArr.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    HomeButtonCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HOTID1" forIndexPath:indexPath];
    
    HomeModel *model = [self.homeArr objectAtIndex:indexPath.row];
    
    cell.model = model;
    
    
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
